package user

import (
	"context"
	log "github.com/sirupsen/logrus"
	"go.mongodb.org/mongo-driver/bson"
	"mongofix/conf"
	"mongofix/model"
	"mongofix/utils"
	"strconv"
	"strings"
)

func UpGroupLimit(ctx context.Context, num int, mid int) {
	userDao := conf.UserDaos[mid%conf.Tia_user_db_num].Database(conf.DB_USER).Collection(utils.GetColForUserByIndex((mid / conf.Tia_user_db_num) % conf.Tia_user_col_num))

	selector := bson.M{"mid": mid}
	update := bson.M{"$set": bson.M{"jgnl": num}}

	result := userDao.FindOneAndUpdate(ctx, selector, update)

	if result.Err() != nil {
		log.Errorf("修改入群限制数量失败:mid:%v", mid)
		return
	}
	user := &model.User{}
	err := result.Decode(user)
	if err != nil {
		log.Errorf("序列化用户信息失败:mid:%v", mid)
		return
	}
	log.Infof("更新入群限制成功：name:%v,已加入群数量：%v,入群限制：%v=>%v", user.Name, user.JoinGroupNum, user.JoinGroupLimit, num)

}

func GetUserIdByMid(ctx context.Context, midsStr string) {

	mids := strings.Split(midsStr, ",")
	for _, midStr := range mids {
		mid, err := strconv.Atoi(midStr)
		if err != nil {
			log.Errorf("midStr => mid fail,midStr:%v", midStr)
			return
		}
		userDao := conf.UserDaos[mid%conf.Tia_user_db_num].Database(conf.DB_USER).Collection(utils.GetColForUserByIndex((mid / conf.Tia_user_db_num) % conf.Tia_user_col_num))
		selector := bson.M{"mid": mid}
		result := userDao.FindOne(ctx, selector)
		user := &model.User{}
		err = result.Decode(user)
		if err != nil {
			log.Errorf("序列化用户信息失败:mid:%v", mid)
			return
		}
		log.Infof("userId:%v,mid:%v", user.UId, user.Mid)
	}
}
